import os
import allure
from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from ..util.log_util import root_path, logger


class Base:     # 基础
    def __init__(self, driver=None):    # 如果未使用,则定义.使用之后,获取之前的driver
        if driver:
            self.driver = driver
        else:
            self.driver = webdriver.Edge()
            self.implicitly_wait(10)
            self.driver.maximize_window()

    def find(self, by):     # 单个
        try:
            el = self.driver.find_element(*by)
            return el
        except NoSuchElementException:
            return False

    def finds(self, by):   # 多个
        try:
            el = self.driver.find_elements(*by)
            return el
        except NoSuchElementException:
            return False

    def find_click(self, by):   # 单个点击按钮
        logger.debug(f'定位点击"{by}"')
        el = self.find(by)
        el.click()

    def finds_click(self, by, num):     # 多个点击按钮
        logger.debug(f'定位点击"{by}[{num}]"')
        el = self.finds(by)
        if 0 < num <= len(el):
            logger.debug(f'输入的num:"{num}"有问题')
            return False
        el[num].click()

    def find_key(self, by, key):    # 单个输入框
        logger.debug(f'定位输入"{by}",值"{key}"')
        el = self.find(by)
        el.send_keys(key)

    def finds_key(self, by, num, key):      # 多个输入框
        logger.debug(f'定位输入"{by}[{num}]",值{key}')
        el = self.finds(by)
        if 0 < num <= len(el):
            logger.debug(f'输入的num:"{num}"有问题')
            return False
        el[num].send_keys(key)

    def find_clear(self, by):   # 单个清空
        logger.debug(f'定位清空"{by}"')
        el = self.find(by)
        el.clear()

    def finds_clear(self, by, num):   # 多个清空
        logger.debug(f'定位清空"{by}[{num}]"')
        el = self.finds(by)
        if 0 < num <= len(el):
            logger.debug(f'输入的num:"{num}"有问题')
            return False
        el[num].clear()

    def find_text(self, by):    # 获取text
        logger.debug(f'获取text"{by}"')
        el = self.find(by)
        return el.text

    def wait(self, method, time_out=5):     # 显式等待,method:*by,time_out等待时间
        logger.debug(f'显式等待定位"{method},时间"{time_out}"')
        el = WebDriverWait(self.driver, time_out).until(expected_conditions.visibility_of_element_located(method))
        return el

    def implicitly_wait(self, time_out=10):     # 隐式等待
        self.driver.implicitly_wait(time_out)

    def images_log(self, img_name, img_allure):     # 截图,img_name保存在本地的图片名字,img-allure上传到allure的名字
        images_dir1 = os.path.dirname(root_path)
        images_dir2 = os.path.join(images_dir1, "images_log")
        os.makedirs(images_dir2, exist_ok=True)
        screenshot_path = os.path.join(images_dir2, f"{img_name}.png")

        try:
            if self.driver.get_screenshot_as_file(screenshot_path):
                allure.attach.file(screenshot_path, name=img_allure,
                                   attachment_type=allure.attachment_type.PNG)
                logger.debug(f'截取当前页面,保存图片"{screenshot_path}",并上传到 allure 中')
        except Exception as e:
            logger.debug(f'截图操作时发生错误"{e}"')

    def file_path(self, file_path):     # 获取文件的绝对路径,test_page为开始输入目录路径
        path = os.path.dirname(root_path)
        file_path = os.path.join(path, file_path)
        file_path = os.path.abspath(file_path)
        logger.debug(f'获取的路径为"{file_path}"')
        return file_path
